<template>
  <div class="container" ref="mainCon">
    <el-row
      class="toolbar searchInputWidth"
      style="padding-bottom: 0; padding-top: 24px !important"
    >
      <el-form :inline="true" :model="filterField" ref="searchConditions">
        <el-col :lg="8" :xl="6">
          <el-form-item label="项目名称" class="row-padding-bottom">
            <el-input
              v-model="filterField.projectName"
              placeholder="请输入项目名称"
              clearable
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :lg="8" :xl="6">
          <el-form-item label="区域" class="row-padding-bottom">
            <el-cascader
              ref="manageArea"
              v-model="areaId"
              :options="areasOptions"
              @change="getAreaId"
              clearable
              filterable
              :props="{ checkStrictly: true }"
              :append-to-body="false"
            ></el-cascader>
          </el-form-item>
        </el-col>
        <template v-if="advanced">
          <el-col :lg="8" :xl="6">
            <el-form-item label="行业" class="row-padding-bottom">
              <el-select
                v-model="filterField.industryId"
                filterable
                placeholder="请选择"
                clearable
                :popper-append-to-body="false"
              >
                <el-option
                  v-for="item in industryOptions"
                  :key="item.code"
                  :label="item.name"
                  :value="item.code"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="8" :xl="6">
            <el-form-item label="专户账号" class="row-padding-bottom">
              <el-input
                v-model="filterField.account"
                placeholder="请输入专户账号"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="8" :xl="6">
            <el-form-item label="开户银行" class="row-padding-bottom">
              <el-select
                v-model="filterField.bankId"
                filterable
                placeholder="请选择开户银行"
                clearable
                @change="bankSelectChange"
                @clear="bankSiteClear"
              >
                <el-option
                  v-for="item in bankOptions"
                  :key="item.code"
                  :label="item.name"
                  :value="item.code"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="8" :xl="6">
            <el-form-item label="网点" class="row-padding-bottom">
              <el-select
                placeholder="请选择银行网点"
                clearable
                v-model="filterField.bankSiteId"
              >
                <el-option
                  v-for="item in bankSiteOptions"
                  :key="item.code"
                  :label="item.name"
                  :value="item.code"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="8" :xl="6">
            <el-form-item label="是否签约" class="row-padding-bottom">
              <el-select
                placeholder="请选择"
                clearable
                v-model="filterField.signed"
              >
                <el-option label="是" value="1"></el-option>
                <el-option label="否" value="0"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="8" :xl="6">
            <el-form-item label="时间" class="row-padding-bottom">
              <timeSelection
                :dateTypes="`month`"
                :startTimeTitle="`起始月份`"
                :endTimeTitle="`截止月份`"
                @getStartTimeValue="getStartTimeValue"
                @getEndTimeValue="getEndTimeValue"
              ></timeSelection>
            </el-form-item>
          </el-col>
        </template>
        <el-col :lg="8" :xl="6">
          <el-form-item label="">
            <a
              @click="toggleAdvanced"
              style="margin-left: 40px; color: #409eff; cursor: pointer"
              class="btnColor"
            >
              {{ advanced ? '收起' : '展开' }}
              <i
                :class="advanced ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"
              ></i>
            </a>
            <el-button
              type="primary"
              icon="icon iconfont iconchazhao1"
              @click="searchList"
              class="searchBtn"
            >
              <span>搜索</span>
            </el-button>
          </el-form-item>
          <el-form-item>
            <exportBtn
              :dataLen="tableList.length"
              :filterField="filterField"
              :api="api"
            ></exportBtn>
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>
    <div class="spacing"></div>
    <div
      ref="tableBox"
      :class="hiddleToolBar ? 'tableBoxCur tableBox' : 'tableBoxCur'"
    >
      <el-table
        ref="tableBox1"
        :max-height="tableHeight"
        :data="tableList"
        v-loading="loading"
        :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
        highlight-current-row
        stripe
        @sort-change="sortChange"
        style="border: 1px solid #dfe6ec"
        border
        @header-dragend="handleDragend"
      >
        <el-table-column prop="projectName" label="项目名称" width="230">
          <template slot-scope="scope">
            <copyText
              :tableIndex="scope.$index"
              :copyTextName="scope.row.projectName"
              :columnName="'projectName'"
            ></copyText>
          </template>
        </el-table-column>
        <el-table-column
          prop="areaDetailDesc"
          label="所属区域"
          :formatter="formatTd"
          width="230"
        >
          <template slot-scope="scope">
            <copyText
              :tableIndex="scope.$index"
              :copyTextName="scope.row.areaDetailDesc"
              :columnName="'areaDetailDesc'"
            ></copyText>
          </template>
        </el-table-column>
        <el-table-column
          prop="industryName"
          label="行业"
          align="center"
          :formatter="formatTd"
        ></el-table-column>
        <el-table-column
          prop="signed"
          label="是否签约"
          align="center"
          :formatter="formatTd"
          width="100"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.signed == 1">是</span>
            <span v-else>否</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="isCancellation"
          label="是否销户"
          align="center"
          :formatter="formatTd"
          width="100"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.isCancellation == 1">是</span>
            <span v-else>否</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="bankName"
          label="开户银行"
          align="center"
          :formatter="formatTd"
          min-width="150"
        ></el-table-column>
        <el-table-column
          label="专户账号"
          align="center"
          :formatter="formatTd"
          min-width="230"
        >
          <template slot-scope="scope">
            <p>{{ scope.row.account || '--' }}</p>
          </template>
        </el-table-column>
        <el-table-column
          prop="bankSiteName"
          label="网点"
          align="left"
          :formatter="formatTd"
          width="230"
        >
          <template slot-scope="scope">
            <copyText
              :tableIndex="scope.$index"
              :copyTextName="scope.row.bankSiteName"
              :columnName="'bankSiteName'"
            ></copyText>
          </template>
        </el-table-column>
        <el-table-column
          label="应发/到账工资总额(元)"
          prop="bankActPay"
          align="center"
          width="200"
        >
          <template slot-scope="scope">
            <p>
              ￥{{ scope.row.shouldPay || '0' }}/￥{{
                scope.row.bankActPay || '0'
              }}
            </p>
          </template>
        </el-table-column>
        <el-table-column
          label="应发/到账总人次"
          prop="bankActPayTotal"
          align="center"
          min-width="180"
        >
          <template slot-scope="scope">
            <p>
              {{ scope.row.shouldPayTotal || '0' }}/{{
                scope.row.bankActPayTotal || '0'
              }}
            </p>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="left" fixed="right" width="110">
          <template slot-scope="scope">
            <el-link
              type="primary"
              v-if="isShowDetail"
              @click="toDetail(scope.$index, scope.row)"
              >查看详情</el-link
            >
            <span v-else>--</span>
          </template>
        </el-table-column>
        <!-- 表格数据为空时，页面显示 -->
        <div slot="empty" v-show="isShow">
          <emptyTable emptyText="暂无相关信息"></emptyTable>
        </div>
      </el-table>
      <Pagination
        ref="pagination"
        @getList="getTableList"
        :filterField="filterField"
        :requestUrl="requestUrl"
      ></Pagination>
    </div>
  </div>
</template>

<script>
import { mapActions, mapState } from 'vuex'
export default {
  name: 'salaryPayList',
  props: ['taskType'],
  data() {
    return {
      titleText: '工资代发',
      advanced: false,
      //表格数据
      tableList: [],
      requestUrl: 'api-su/cs/salarystats/expand/list',
      areaId: '',
      bankOptions: [],
      //搜索条件
      filterField: {
        projectName: '', //项目名称
        areaId: '', //区域
        industryId: '', //行业
        account: '', //专户账号
        bankId: '', //银行
        serverArea: '', // 服务部署区域
        signed: '', //是否签约
        bankSiteId: '',
        salaryStartDate: '',
        salaryEndDate: '',
      },
      isShow: false, //列表加载
      loading: true,
      isShowDetail: '',
      bankId: '',
      bankSiteOptions: [],
      api: '',
      tableHeight: null,
      hiddleToolBar: false,
    }
  },
  computed: { ...mapState(['areasOptions', 'industryOptions']) },

  created() {
    this.getBank()
    this.getUserType()
    this.getAreas({ areaId: this.$store.state.areaInfoArr.areaId })
    this.$nextTick(function () {
      this.$refs.pagination.getTableListData()
    })
    this.getIndustry({})
    this.getApi()
    this.filterField.serverArea = this.$store.state.areaInfoArr.areaId
    this.isShowDetail =
      this.$store.state.buttonLimitsArr.indexOf('bankSalary:detail') > -1 ||
      this.$store.state.buttonLimitsArr.indexOf('mCsSalaryList:detail') > -1 ||
      this.$store.state.buttonLimitsArr.indexOf('bankSalaryPayList:detail') >
        -1 ||
      this.$store.state.buttonLimitsArr.indexOf('TBankSalaryPayList:detail') >
        -1
  },
  activated() {
    window.addEventListener('scroll', this.handleScroll, true)
    this.$nextTick(function () {
      this.$refs.pagination.getTableListData()
      this.$refs.tableBox1.doLayout()
    })
  },
  deactivated() {
    let that = this
    window.removeEventListener('scroll', that.handleScroll, true)
  },
  beforeDestroy() {
    let that = this
    window.removeEventListener('scroll', that.handleScroll, true)
  },
  mounted() {
    window.addEventListener('scroll', this.handleScroll, true)
    let that = this
    this.$nextTick(function () {
      let tableHeightCur = this.$refs.tableBox1.$el.clientHeight
      let tableTop = this.$refs.tableBox.getBoundingClientRect().top
      if (tableHeightCur + tableTop > window.innerHeight) {
        this.tableHeight =
          this.$refs.mainCon.getBoundingClientRect().height - 86
      } else {
        document.getElementsByClassName('tableBoxCur').length &&
          (document.getElementsByClassName('tableBoxCur')[0].style.height =
            window.innerHeight - tableTop - 28 + 'px')
      }

      window.onresize = function () {
        if (!that.$refs.tableBox1) {
          return
        }
        let tableHeightCur = that.$refs.tableBox1.$el.clientHeight
        let tableTop = that.$refs.tableBox.getBoundingClientRect().top
        if (tableHeightCur + tableTop > window.innerHeight) {
          that.tableHeight =
            that.$refs.mainCon.getBoundingClientRect().height - 86
        } else {
          document.getElementsByClassName('tableBoxCur').length &&
            (document.getElementsByClassName('tableBoxCur')[0].style.height =
              window.innerHeight - tableTop - 40 + 'px')
        }
      }
    })
  },
  methods: {
    /**
     * @description: 获取收件起始日期
     * @param {*} val
     */
    getStartTimeValue(val) {
      if (val) {
        this.filterField.salaryStartDate = val
      } else {
        this.filterField.salaryStartDate = ''
      }
    },
    /**
     * @description: 获取收件结束日期
     * @param {*} val
     */
    getEndTimeValue(val) {
      if (val) {
        this.filterField.salaryEndDate = val
      } else {
        this.filterField.salaryEndDate = ''
      }
    },
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    ...mapActions(['getAreas', 'getIndustry']),
    getBank() {
      var data = {
        type: 'BANK_TYPE',
      }
      this.axios({
        method: 'post',
        url: 'api-bs/base/dicitem/findbytype',
        data: data,
      })
        .then((response) => {
          this.bankOptions = []
          if (this.bankId) {
            for (let i = 0; i < response.data.result.length; i++) {
              let bank = response.data.result[i]
              if (this.bankId == bank.code) {
                this.bankOptions.push(bank)
              }
            }
          } else {
            this.bankOptions = response.data.result
          }
        })
        .catch((error) => {
          console.log(error)
        })
    },
    getTableList(data) {
      this.tableList = data
      this.loading = false
      if (this.tableList.length < 1) {
        this.isShow = true
      }
    },
    //获取选中的区域最后一级id
    getAreaId() {
      var areaId = this.areaId[this.areaId.length - 1]
      this.filterField.areaId = areaId
    },

    // 搜索
    searchList() {
      this.$refs.pagination.getTableListData()
    },
    //跳转详情
    toDetail(index, row) {
      this.$router.push({
        path: '/home/salaryDf/salaryPayBatchList',
        name: 'salaryPayBatchList',
        query: {
          projectId: row.projectId,
          projectName: row.projectName,
          taskType: this.taskType,
        },
      })
      this.tabChoose()
    },
    getUserType() {
      this.axios({
        method: 'get',
        url: 'api-u/users/current',
      })
        .then((response) => {
          //用户类型
          this.bankId = response.data.bankId
          this.getBank()
        })
        .catch((resp) => {})
    },
    getBankSite(bankId) {
      this.axios({
        method: 'post',
        url: 'api-acc/accountbank/expand/banksite',
        data: { bankId: bankId },
      })
        .then((response) => {
          this.bankSiteOptions = response.data.result
        })
        .catch((error) => {})
    },
    bankSelectChange(val) {
      if (val) {
        this.getBankSite(val)
      }
    },
    bankSiteClear() {
      this.bankSiteOptions = []
      this.filterField.bankSiteId = ''
    },
    getApi() {
      let loginType = localStorage.getItem('loginType')
      if (loginType === 'JC') {
        this.api = 'api-s/report/export_JC_CS_POXY_PAY_SALARY'
      } else if (loginType === 'BANK') {
        this.api = 'api-s/report/export_YH_ACCOUNT_SALARY_AGENCY'
      }
    },
    tabChoose() {
      var oldUrl =
        this.taskType == 'salary'
          ? '/home/salaryDf/salaryPayListTabTwo'
          : '/home/salaryDf/salaryPayListTab'
      this.$store.commit('CHANGE_SLIDER_NAME', {
        oldUrl: oldUrl,
        newUrl: this.$router.currentRoute.fullPath,
      })
    },
    handleScroll(e) {
      this.$nextTick(function () {
        if (
          this.$refs.tableBox &&
          this.$refs.tableBox.getBoundingClientRect().top < 128
        ) {
          // this.tableHeight = this.$refs.mainCon.getBoundingClientRect().height - 84
          this.tableHeight = window.innerHeight - 218

          if (document.getElementsByClassName('tableBoxCur').length) {
            document.getElementsByClassName('tableBoxCur')[0].style.height =
              null
          }
          this.hiddleToolBar = true
        } else {
          this.hiddleToolBar = false
        }
      })
    },
  },
}
</script>

<style scoped lang="less">
// .container {
// padding:24px;
// }
.topTitle {
  height: 67px;
  background-color: #e4ebf1;
  padding-left: 24px;
  // font: bold 18px/67px Microsoft YaHei;
}
.mainCon {
  top: 198px;
}
@media only screen and (max-width: 1366px) {
  /deep/.searchInputWidth .el-form--inline .el-form-item__label {
    width: 80px;
  }
}
@import '../../../static/css/filterArea.less';
</style>
